﻿using Daemon.Model;
using Microsoft.AspNetCore.Mvc;
using Daemon.Repository.Contract;
using System.Net.Http;
using Microsoft.AspNetCore.Http;
using Daemon.Common;
using Daemon.Service.Contract;
using System.Linq;
using System;
using Daemon.Data.Substructure.Enums;
using Daemon.Common.Exceptions;
using System.Net;

namespace DaemonAPI
{
	[ApiController]
	[Route("api/[controller]/[action]")]
	public class TakeAwayUserAccountController : BaseApiController<TakeAwayUserAccount, ITakeAwayUserAccountRepository>
	{
		private readonly ITakeAwayIntegeralSettingRepository _takeAwayIntegeralSettingRepository;

		private readonly ITakeAwayUserOrderRepository _takeAwayUserOrderRepository;

		private readonly ITakeAwayUserAccountRepository _takeAwayUserAccountRepository;

		private readonly ITakeAwayAccountTransactionRepository _takeAwayAccountTransactionRepository;

		private readonly ITakeAwayIntegeralTransactionRepository _takeAwayIntegeralTransactionRepository;

		public TakeAwayUserAccountController(ITakeAwayUserAccountRepository takeAwayUserAccountRepository,
			ITakeAwayIntegeralTransactionRepository takeAwayIntegeralTransactionRepository,
			ITakeAwayAccountTransactionRepository takeAwayAccountTransactionRepository, ITakeAwayUserOrderRepository takeAwayUserOrderRepository, ITakeAwayIntegeralSettingRepository takeAwayIntegeralSettingRepository, ITakeAwayUserAccountRepository repository, IHttpContextAccessor httpContextAccessor, ITakeAwayUserBookService takeAwayUserBookService) : base(repository, httpContextAccessor)
		{
			_takeAwayIntegeralSettingRepository = takeAwayIntegeralSettingRepository;
			_takeAwayUserOrderRepository = takeAwayUserOrderRepository;
			_takeAwayUserAccountRepository = takeAwayUserAccountRepository;
			_takeAwayAccountTransactionRepository = takeAwayAccountTransactionRepository;
			_takeAwayIntegeralTransactionRepository = takeAwayIntegeralTransactionRepository;
		}

		[HttpPost]
		[Route("")]
		public ResultModel SubmitBalanceOrder(int orderId)
		{
			var currentOrder = _takeAwayUserOrderRepository.FindById(orderId);
			if (currentOrder.Status != (int)UserOrderStatusEnum.UnPay)
			{
				throw new BusinessException((int)HttpStatusCode.BadRequest, "支付失败！请重新下单");
			}

			var payResult = UpdateUserAccount(currentOrder);
			if (!payResult)
			{
				currentOrder.Status = (int)UserOrderStatusEnum.Cancel;
				_takeAwayUserOrderRepository.UpdateByDBContext(currentOrder);
				throw new BusinessException((int)HttpStatusCode.BadRequest, "支付失败！积分不足");
			}

			currentOrder.PayType = 1;
			currentOrder.Status = (int)UserOrderStatusEnum.Open;
			return new ResultModel(System.Net.HttpStatusCode.OK, "支付成功！", _takeAwayUserOrderRepository.UpdateUserOrder(currentOrder));
		}

		[HttpGet]
		[Route("")]
		public ResultModel GetUserAccountInfo(int userId)
		{
			var userIntegeral = _takeAwayIntegeralTransactionRepository.FindAll().Where(r => r.UserId == userId).ToList();
			var userMoney = _takeAwayIntegeralTransactionRepository.FindAll().Where(r => r.UserId == userId).ToList();
			return new ResultModel(HttpStatusCode.OK, null, new { RemainingIntegeral = userIntegeral.Sum(r => r.AdjustCost), RemainingBalance = userMoney.Sum(r => r.AdjustCost) });
		}

		private bool UpdateUserAccount(TakeAwayUserOrder entity)
		{
			return UpdateUserAccountIntegeral(entity);
		}

		private bool UpdateUserAccountIntegeral(TakeAwayUserOrder entity)
		{
			var userAccount = _takeAwayUserAccountRepository.FindAll().FirstOrDefault(r => r.UserId == entity.UserId);
			var userAccountIntegeral = _takeAwayIntegeralTransactionRepository.FindAll().Where(r => r.UserId == entity.UserId).Select(r => r.AdjustCost).Sum();
			if (userAccount == null)
			{
				return false;
			}

			var userUsedIntegeral = Convert.ToInt32(entity.Price);
			if (userUsedIntegeral > userAccountIntegeral)
			{
				return false;
			}

			var integeralTransaction = new TakeAwayIntegeralTransaction() { OrderId = entity.Id, AccountId = userAccount.Id, AdjustCost = -userUsedIntegeral, ShopId = entity.ShopId, UserId = userAccount.UserId, Description = $"用户{{0}}消费了{userUsedIntegeral}元钱,积分-{userUsedIntegeral}" };
			_takeAwayIntegeralTransactionRepository.AddByDBContext(integeralTransaction);
			return true;
		}
	}
}
